<?php
class Proveedores_model extends CI_Model {
    public function __construct() {
        $this->load->database();
    }
    
    public function get_proveedores() {
        $query = $this->db->query("SELECT *
                                    FROM
                                        proveedores
                                    ORDER BY
                                        nombre");
        return $query->result_array();
    }
    
    public function existe_proveedor_por_nombre($nombre) {
        $query = $this->db->query("SELECT count(*) as cant
                                    FROM
                                        proveedores
                                    WHERE
                                        nombre = '$nombre'");
        $res = $query->row_array();
        
        if($res['cant'] == 0) {
            return false;
        } else {
            return true;
        }
    }
    
    public function set_proveedor($datos) {
        $this->db->insert('proveedores', $datos);
    }
    
    public function get_proveedor_por_id($id) {
        $query = $this->db->query("SELECT *
                                    FROM
                                        proveedores
                                    WHERE
                                        idProveedor = $id");
        return $query->row_array();
    }
    
    public function update($datos, $id) {
        $id = array(
            'idProveedor' => $id
        );
        
        $this->db->update('proveedores', $datos, $id);
    }
    
    public function get_cantidad_de_proveedores() {
        $query = $this->db->query("SELECT COUNT(*) as cant
                                    FROM
                                        proveedores");
        return $query->row_array();
    }
    
    public function get_proveedores_con_pedidos_pendientes() {
        $query = $this->db->query("SELECT p.*
                                    FROM
                                        proveedores p,
                                        consultas c,
                                        itemsconsulta ic,
                                        articulos a,
                                        itemspedido ip
                                    WHERE
                                        p.idProveedor = c.proveedor AND
                                        c.idconsulta = ic.idconsulta AND
                                        ic.idarticulo = a.idArticulo AND
                                        a.idArticulo = ip.idArticulo AND
                                        ip.pendiente = 1
                                    GROUP BY
                                        p.idProveedor
                                    ORDER BY
                                        p.nombre");
        
        return $query->result_array();
    }
}
?>
